var toolbarOptions = [
      ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
      ['blockquote', 'code-block'],

      [{ 'header': 1 }, { 'header': 2 }],               // custom button values
      [{ 'list': 'ordered'}, { 'list': 'bullet' }],
      [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
      [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
      [{ 'direction': 'rtl' }],                         // text direction

      [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
      [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

      [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
      [{ 'font': [] }],
      [{ 'align': [] }],

      ['clean']                                         // remove formatting button
    ];


function saveOrUpdateCate(mode){
    var name = $("#new_cate_name").val();
    var shortcut = $("#new_cate_shortcut").val();
    var price = $("#new_cate_price").val();
    var vipPrice = $("#new_cate_vip_price").val();
    var category = $("#c_"+randomId).val();
    var description = $("#new_cate_description > div.ql-editor").html();
    var hasError = false;
    if(name == ""){
        $("#new_cate_name").parent().addClass("has-error");
        hasError = true;
    }
    if(shortcut == ""){
        $("#new_cate_shortcut").parent().addClass("has-error");
        hasError = true;
    }
    if(price == ""){
        $("#new_cate_price").parent().addClass("has-error");
        hasError = true;
    }
    if(category == null || category == ""){
        $("#c_"+randomId).parent().addClass("has-error");
        hasError = true;
    }
    if(hasError){
        return false;
    }
    var url = "";
    var id = "";
    if(mode == 1)
        url = "/cate/create";
    else if(mode == 2){
        url = "/cate/update";
        id = $("#cate_id").val();
     }
     else return false;

    $.ajax({
        url:url,
        type:'post',
        dataType:'json',
        data:{'id':id,'name':name,'shortcut':shortcut,'price':price,'vipPrice':vipPrice,'category':category,'description':description},
        success:function(data){
            if(data.success){
                alert("Save cate success");
                window.location.href = data.redirectUrl;
            }else{
                alert(data.errMsg);
            }
        },
        error:function(er){
            alert("Save cate error");
            console.log(JSON.stringify(er));
        }
    });
}

function onDeleteDetailImage(e){
    var data = $(e).data();
    var id =data["id"];
    var img = data["img"];
    if(confirm("Do you sure delete this image?")){
        $.ajax({
            url:'/cate/delete_img',
            type:'post',
            dataType:'json',
            data:{'id':id,'img':img},
            success:function(data){
                if(data.success){
                    alert("Delete image success");
                    $(e).parent().parent().remove();
                }else{
                    alert(data.errMsg)
                }
            },
            error:function(er){
                alert("Delete image error.");
                console.log(JSON.stringify(er));
            }
        });
    }
}

function onAddToCart(e){
    var dataSet = $(e).data();

    var userToken = $("#user_token").val();
    var cateId = dataSet["id"];
    $.ajax({
        url:'/cate/add_to_cart',
        type:'post',
        dataType:'json',
        data:{'cate_id':cateId,'user_token':userToken},
        success:function(data){
            if(data.success){
                var countInCart = parseInt($("#countInCart").text());
                $("#countInCart").text((countInCart+1));
            }
        },
        error:function(er){

        }
    });
}